package com.atguigu.oss.controller;

import com.atguigu.common.util.R;
import com.atguigu.oss.service.OSSService;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.HashMap;

@RestController
@CrossOrigin
@RequestMapping("/api/oss")
public class OSSController {

    @Resource
    private OSSService ossService;

    @RequestMapping("/")
    public Object index() {
        HashMap<String, Object> result = new HashMap<>();
        result.put("service-name", "/api/oss");
        result.put("success", "ok");
        return result;
    }

    @PostMapping("/file/upload")
    public R upload(MultipartFile file) {
        String url = ossService.uploadFile(file);
        return R.ok().message("上传成功").data("url", url);
    }

    @PostMapping("/avatar/upload")
    public R uploadAvatar(MultipartFile file) {
        String url = ossService.uploadAvatar(file);
        return R.ok().message("上传成功").data("url", url);
    }
}
